package pres.wchen.oa.cor.shiro.web;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.github.pagehelper.PageInfo;

import org.springframework.web.servlet.ModelAndView;
import pres.wchen.oa.cor.shiro.model.BaseModel;
import pres.wchen.oa.cor.shiro.model.SysUser;
import pres.wchen.oa.cor.shiro.service.SysUserService;

/**
 * 源文件名：SysUserController.java
 * 作者：wchen
 * 创建时间：2018/1/13
 * 文件描述：用户角色表
 * 版权所有：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息： 
 */
@RestController
@RequestMapping("/user")
public class SysUserController extends BaseController {
	@Resource
	private SysUserService sysUserService;


	/**
	 * @方法描述 主页
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/index")
	public ModelAndView index(SysUser sysUser) {
		return super.dealInfoResult("index", null);
	}

	/**
	 * @方法描述 主页
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/index2")
	public ModelAndView userIndex(HttpSession session, Integer pageNo, Integer pageSize) {
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		BaseModel baseModel = sysUserService.list(conditionMap);
		return super.dealListResult("/role/user", baseModel);
	}


	/**
	 * @方法描述 新增
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值 
	 * @创建日期 2018/1/13
	 */ 
	@PostMapping(value = "/")
	public Map insert(SysUser sysUser) {
		Long id = sysUserService.insert(sysUser);
		return super.dealInsertResult(id);
	}


	/**
	 * @方法描述 删除
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值 
	 * @创建日期 2018/1/13
	 */ 
	@DeleteMapping(value = "/{id}")
	public Map delete(@PathVariable("id") Long id) {
		Integer result = sysUserService.delete(id);
		return super.dealDeleteResult(result);
	}


	/**
	 * @方法描述 更新
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值 
	 * @创建日期 2018/1/13
	 */ 
	@PutMapping(value = "/")
	public Map update(SysUser sysUser) {
		Integer result = sysUserService.update(sysUser);
		return super.dealUpdateResult(result);
	}


	/**
	 * @方法描述 详情
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值 
	 * @创建日期 2018/1/13
	 */ 
	@GetMapping(value = "/{id}")
	public ModelAndView info(@PathVariable("id") Long id) {
		SysUser sysUser = sysUserService.info(id);
		return super.dealInfoResult("user/index", sysUser);
	}


	/**
	 * @方法描述 列表
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值 
	 * @创建日期 2018/1/13
	 */ 
	@PostMapping(value = "/list")
	public Map list(Map map) {
		BaseModel baseModel = sysUserService.list(map);
		return super.dealListResult(baseModel);
	}

	/**
	 * @方法描述 用户列表分页
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/pageList")
	public ModelAndView userpageList(HttpServletRequest request) {
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", request.getParameter("pageSize"));
		conditionMap.put("pageNo", request.getParameter("pageNo"));
		conditionMap.put("username", request.getParameter("username"));
		conditionMap.put("pageFlag", request.getParameter("pageFlag"));
		BaseModel baseModel = sysUserService.list(conditionMap);
		return super.dealListResult("/role/user_table", baseModel);
	}



	/**
	 * @方法描述 角色分配分页列表
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/allotRoles/pageList")
	public Map pageList(HttpServletRequest request) {
		Object pageNo = request.getParameter("pageNo");
		Object pageSize = request.getParameter("pageSize");
		Object username = request.getParameter("username");
		Object pageFlag = request.getParameter("pageFlag");
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		conditionMap.put("username", username);
		conditionMap.put("pageFlag", pageFlag);
		BaseModel baseModel = sysUserService.list(conditionMap);
		return super.dealListResult( baseModel);
	}


	/**
	 * @方法描述 角色分配主页
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/allotRoles/index")
	public ModelAndView index(HttpSession session, Integer pageNo, Integer pageSize) {
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		BaseModel baseModel = sysUserService.list(conditionMap);
		return super.dealListResult("/role/allot_role", baseModel);
	}


	/**
	 * @方法描述 分配角色
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@PostMapping(value = "/allotRoles/addRoles")
	public Map allotUserRoles(@RequestBody Map map) {
		Long userId = Long.valueOf(map.get("userId").toString());
		List roleIdList = (List) map.get("roleIdList");
		//清除原有权限
		sysUserService.deleteUserRoles(userId);

		//添加赋予的权限
		if(roleIdList.size()> 0) {
			sysUserService.addUserRoles(map);
		}
		return super.dealInfoResult(null);
	}

}